#property copyright ""
#property link      "mingxinhe@gmail.com"

#property indicator_chart_window
#property indicator_maximum 10
#property indicator_minimum -10

extern color  UpColor = Green; 
extern color  DownColor = Red;

int digitFactor = 1;

double topBoxPrice, bottomBoxPrice, lastTopBoxPrice, lastBottomBoxPrice;
datetime lastTime;
double touchLinePrice;
int recId;
string id;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void init() 
{
   DeleteObjects();
      
   if(Bid<10 && Digits==5) { digitFactor=10;}
   if(Bid<10 && Digits==4) { digitFactor= 1;}

   if(Bid>10 && Digits==3) { digitFactor=10;}
   if(Bid>10 && Digits==2) { digitFactor= 1;} 
      
   IndicatorShortName("Boxes");   
   
   lastTime = Time[0];   
  
}

//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
void deinit() 
{
    Comment("");
    DeleteObjects();
    ObjectsDeleteAll(0, OBJ_TEXT);
    return(0);
}

//+------------------------------------------------------------------+
//| Remove all Rectangles                                            |
//+------------------------------------------------------------------+
void DeleteObjects() 
{
    ObjectsDeleteAll(0,OBJ_RECTANGLE);     
    return(0); 
}

void start() 
{   
    int tempInt = Close[0]/Point - MathFloor(Close[0]/Point/(100*digitFactor))*(100*digitFactor);

    int botInt = MathFloor(tempInt/(12.5*digitFactor));
    int topInt = MathCeil(tempInt/(12.5*digitFactor));
    
    //if (botInt % 2 == 0) botInt = botInt - 1;
    //if (topInt % 2 == 0) topInt = topInt + 1;     
    
    topBoxPrice = (MathFloor(Close[0]/Point/(100*digitFactor))*(100*digitFactor) + topInt*12.5*digitFactor)*Point;
    bottomBoxPrice = (MathFloor(Close[0]/Point/(100*digitFactor))*(100*digitFactor) + botInt*12.5*digitFactor)*Point;
    
    if (topBoxPrice != lastTopBoxPrice && bottomBoxPrice != lastBottomBoxPrice)
    {
        if (Low[1] > lastBottomBoxPrice && 
           Close[0] <= lastBottomBoxPrice && 
           lastBottomBoxPrice > 0 && 
           Time[0] > lastTime)
        {
            if (touchLinePrice != lastBottomBoxPrice)
            {
                recId++;
                id =  recId;
                ObjectCreate(id, OBJ_RECTANGLE, 0, 0, 0, 0, 0);
                ObjectSet(id, OBJPROP_TIME1 , lastTime);
                ObjectSet(id, OBJPROP_TIME2 , Time[0]);
                ObjectSet(id, OBJPROP_PRICE1, lastTopBoxPrice);  
                ObjectSet(id, OBJPROP_PRICE2, lastBottomBoxPrice);
                ObjectSet(id, OBJPROP_STYLE, STYLE_SOLID);      
                ObjectSet(id, OBJPROP_BACK, True);
                ObjectSet(id, OBJPROP_COLOR, DownColor);
                touchLinePrice = lastBottomBoxPrice;
            }
            else
            {
                ObjectSet(id, OBJPROP_TIME2 , Time[0]);
            }
            
            lastTime = Time[0];
            Print("go down touch ", DoubleToStr(lastBottomBoxPrice, Digits), " at ", TimeToStr(TimeCurrent()));
        }  
   
        if (High[1] < lastTopBoxPrice && 
            Close[0] >= lastTopBoxPrice && 
            lastTopBoxPrice > 0 && 
            Time[0] > lastTime)
        {
            if (touchLinePrice != lastTopBoxPrice)
            {
                recId++;
                id = recId;
                ObjectCreate(id, OBJ_RECTANGLE, 0, 0, 0, 0, 0);
                ObjectSet(id, OBJPROP_TIME1 , lastTime);
                ObjectSet(id, OBJPROP_TIME2 , Time[0]);
                ObjectSet(id, OBJPROP_PRICE1, lastTopBoxPrice);  
                ObjectSet(id, OBJPROP_PRICE2, lastBottomBoxPrice);
                ObjectSet(id, OBJPROP_STYLE, STYLE_SOLID);      
                ObjectSet(id, OBJPROP_BACK, True);
                ObjectSet(id, OBJPROP_COLOR, UpColor); 
                touchLinePrice = lastTopBoxPrice;
            }
            else
            {
                ObjectSet(id, OBJPROP_TIME2 , Time[0]);
            }
            
            lastTime = Time[0];
            Print("go up touch ", DoubleToStr(lastTopBoxPrice, Digits), " at ", TimeToStr(TimeCurrent()));
        } 
        
        lastTopBoxPrice = topBoxPrice;  
        lastBottomBoxPrice = bottomBoxPrice;
    }
    
    Comment("topBoxPrice :", DoubleToStr(topBoxPrice, Digits), " bottomBoxPrice:", DoubleToStr(bottomBoxPrice, Digits));  
}